// android does not support openal so this is mainly a skeleton
#ifdef ANDROID_PLATFORM

#include "VxIrrLib.h"
//============================================================================
//=== audio source ===//
//============================================================================
//=== constructor ===//
AudioSource::AudioSource(	const AudioBuffer * poAudioBuffer, 
							BOOL bLoop, 
							F32 f32MinGain, 
							F32 f32MaxGain, 
							F32 f32ReferenceDistance, 
							F32 f32RollOff  )
{
	m_bLoaded = false;
	if( poAudioBuffer )
	{

//		// generate id
//		alGenSources( 1, &m_u32ID );

//		// Assign buffer to source
//		alSourcei( m_u32ID, AL_BUFFER, poAudioBuffer-> m_u32ID );
//		alSourcef( m_u32ID, AL_MIN_GAIN, f32MinGain );
//		alSourcef( m_u32ID, AL_MAX_GAIN, f32MaxGain );
//		alSourcef( m_u32ID, AL_REFERENCE_DISTANCE, f32ReferenceDistance );
//		alSourcef( m_u32ID, AL_ROLLOFF_FACTOR, f32RollOff );
//		alSourcei( m_u32ID, AL_LOOPING, bLoop);

		m_u32ID = 1; // for now fake a id
		m_bLoop					= bLoop;
		m_f32MinGain			= f32MinGain;
		m_f32MaxGain			= f32MaxGain;
		m_f32ReferenceDistance	= f32ReferenceDistance;
		m_f32RollOff			= f32RollOff;

		m_strSndFile = poAudioBuffer->m_strSndFile;
		log_msg( 0, "AudioSource: created with snd %s\n", m_strSndFile.c_str() );

		m_bLoaded = true;
	}
	else
	{
		log_msg( 0, "AudioSource: NULL SOUND FILE\n" );
	}
}
//============================================================================
//=== destructor ===//
AudioSource::~AudioSource()
{
}

//============================================================================
void AudioSource::SetGain(float f32Value)
{
}
//============================================================================
void AudioSource::SetPosition( float f32X, float f32Y, float f32Z )
{
}
//============================================================================
void AudioSource::SetPitch(float f32Value)
{
}
//============================================================================
void AudioSource::Play( void )
{
	if( 0 == m_strSndFile.size() )
	{
		log_msg( 0, "AudioSource::Play: null sound file\n" );
		return;
	}
	// android does not have openal.. call into java to play
	log_msg( 1, "AudioSource::Play: playing file %s\n", m_strSndFile.c_str()  );
	g_oSndMgr.SfxPlay( m_strSndFile.c_str() );

}

//============================================================================
//=== audio base ===//
//============================================================================
//============================================================================
//=== constructor ===//
AudioBase::AudioBase()
	: m_bEnabled(0)
{
}
//============================================================================
//=== destructor ===//
AudioBase::~AudioBase()
{
}

//============================================================================
//! startup
RCODE AudioBase::Startup( BOOL bEnabled )
{
	log_msg( 0, "AudioBase::Startup: enable=%d\n", bEnabled );
	m_bEnabled = bEnabled;
	if( false == m_bEnabled )
	{
		return -1;
	}
	return 0;
}
//============================================================================
//! shutdown
void AudioBase::Shutdown( void )
{
}


//============================================================================
//=== methods ===//
//============================================================================
//============================================================================
//! load audio buffer from sound file
RCODE AudioBase::LoadBuffer( const irr::core::stringc& strFile )
{
	if( strFile.size() < 4 )
	{
		log_msg( 1, "AudioBase::LoadBuffer: file name too short \n" );
		return -1;
	}
	AudioBuffer oAudioBuffer;

	oAudioBuffer.m_strSndFile = strFile;

	// put buffer info into mapped pair ( path, buffer )
	m_aoAudioBuffers[ strFile ] = oAudioBuffer;
	log_msg( 0, "AudioBase::LoadBuffer: loaded %s\n", strFile.c_str() );


	return 0;
}
//============================================================================
//! get audio buffer that is loaded from sound file
const AudioBuffer *	 AudioBase::GetBuffer( const irr::core::stringc & strFile)
{
	//log_msg( 1, "AudioBase::GetBuffer: getting buffer %s\n", strFile.c_str() );
	if( false == m_bEnabled )
	{
		log_msg( 1, "AudioBase::GetBuffer: Sound not enabled.. not loading file %s\n", strFile.c_str() );
		return NULL;
	}
	// find buffer
	AudioBuffersIterator = m_aoAudioBuffers.find( strFile );
	if( AudioBuffersIterator == m_aoAudioBuffers.end() )
	{
		// no buffer with given path and file exists in our list
		// attempt to load
		log_msg( 1, "AudioBase::GetBuffer: could not find %s.. attempting load\n", strFile.c_str() );
		RCODE rc = LoadBuffer( strFile );
		if( rc )
		{
			log_msg( 1, "AudioBase::GetBuffer: load ERROR %d\n", rc );
			return NULL;
		}
		AudioBuffersIterator = m_aoAudioBuffers.find( strFile );
		if( AudioBuffersIterator == m_aoAudioBuffers.end() )
		{
			log_msg( 1, "AudioBase::GetBuffer: FAIL: could not find %s with reload.. attempting load\n", strFile.c_str() );
			return NULL;
		}
		//else
		//{
		//	log_msg( 1, "AudioBase::GetBuffer: LOAD SUCCESS %s\n", strFile.c_str() );
		//}
	}
	//log_msg( 1, "AudioBase::GetBuffer: FOUND BUFFER FOR %s\n", strFile.c_str() );
	return &AudioBuffersIterator->second;
}
//============================================================================
//! free all sound buffers
void AudioBase::FreeAllBuffers( void )
{
}
//============================================================================
//! set position of sound listener
void AudioBase::SetListenPosition( F32 f32X, F32 f32Y, F32 f32Z )
{
	if( false == m_bEnabled )
	{
		return;
	}
	//alListener3f( AL_POSITION, f32X, f32Y, -f32Z);
}
//============================================================================
//! set position of sound listener ( irrlicht coords )
void AudioBase::SetListenPosition( irr::core::vector3df& v3fPos )
{
	if( false == m_bEnabled )
	{
		return;
	}
	//alListener3f( AL_POSITION, v3fPos.X, v3fPos.Y, -v3fPos.Z );
}
//============================================================================
//! set position of sound listener ( bullet physics coords )
void AudioBase::SetListenPosition( btVector3& vec3Pos )
{
	if( false == m_bEnabled )
	{
		return;
	}
	//alListener3f( AL_POSITION, vec3Pos[0], vec3Pos[1], -vec3Pos[2] );
}
//============================================================================
//! set direction listener if facing
void AudioBase::SetListenDirection( F32 f32X, F32 f32Y, F32 f32Z )
{
	if( false == m_bEnabled )
	{
		return;
	}
	F32 af32Orientation[6] = { f32X, f32Y, -f32Z, 0.0f, 1.0f, 0.0f };
	//alListenerfv( AL_ORIENTATION, af32Orientation );
}
//============================================================================
//! set direction of sound listener ( irrlicht coords )
void AudioBase::SetListenDirection( irr::core::vector3df& v3fDir )
{
	if( false == m_bEnabled )
	{
		return;
	}
	F32 af32Orientation[6] = { v3fDir.X, v3fDir.Y, -v3fDir.Z, 0.0f, 1.0f, 0.0f };
	//alListenerfv( AL_ORIENTATION, af32Orientation );
}
//============================================================================
//! set direction of sound listener ( bullet physics coords )
void AudioBase::SetListenDirection( btVector3& vec3Dir )
{
	if( false == m_bEnabled )
	{
		return;
	}
	F32 af32Orientation[6] = { vec3Dir[0], vec3Dir[1], -vec3Dir[2], 0.0f, 1.0f, 0.0f };
	//alListenerfv( AL_ORIENTATION, af32Orientation );
}

#endif // ANDROID_PLATFORM
